package com.zr.plant.service.impl;

import com.zr.planclass.util.AllRecords;
import com.zr.plant.mapper.PlantMapper;
import com.zr.plant.model.*;
import com.zr.plant.service.PlantService;
import com.zr.util.ResultBuilderVo;
import com.zr.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by lenovo on 2019/12/25.
 */
@Service
public class PlantServiceimpl  implements PlantService{
    @Autowired
    private PlantMapper plantMapper;
    @Override

    public ResultVo queryLegalPersonById(Integer userid){
        List<LegalPersonVo> legalPersonVoList =  plantMapper.queryLegalPersonById(userid);
        return ResultBuilderVo.success(legalPersonVoList);
    }

    @Override
    public ResultVo queryPage(PlantSelectVo plantSelectVo) {
        AllRecords allRecords = new AllRecords();
        //根据条件查询总条数
        int count =  plantMapper.queryCount(plantSelectVo);
        //根据条件查询数据
        List<PlantVo> plantVoList = plantMapper.queryData(plantSelectVo);
       /* for (PlantVo plantVo:plantVoList){
            if (plantVo.getStatus()!=null){
                if (!plantVo.getStatus()){

                    plantVo.setStatusName("禁用");
                }else {
                    plantVo.setStatusName("启用");
                }
            }

        }*/
        allRecords.setPageIndex(plantSelectVo.getPageIndex());
        allRecords.setPageSize(plantSelectVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.setDataList(plantVoList);
        allRecords.resetTotalNumber(count);
        return ResultBuilderVo.success(allRecords);
    }

    @Override
    @Transactional
    public ResultVo add(@Valid PlantAddVo plantAddVo) {
    //验证法人是否存在，验证工厂是否重复
        ResultVo resultVo = validate(plantAddVo);
        if (!resultVo.getSuccess()){
            return resultVo;
        }
        //数据赋值
        PlantVo plantVo = new PlantVo();
        BeanUtils.copyProperties(plantAddVo, plantVo);
        Date nowDate = new Date();
        plantVo.setCreateName("小明");
        plantVo.setCreateTime(nowDate);
        plantVo.setUpdateName("小明");
        plantVo.setUpdateTime(nowDate);
        //入库
        plantMapper.insertPlant(plantVo);
        return ResultBuilderVo.success();
    }

    @Override
    @Transactional
    public ResultVo update(@Valid PlantUpdateVo plantUpdateVo) {
        //根据工厂code查询工厂信息
         PlantVo plantVo = plantMapper.queryPlantByCode(plantUpdateVo.getLegalPlantCode());
         if (plantVo == null){
             return ResultBuilderVo.error("修改数据不存在！");
         }
         //验证数据状态是否为禁用状态表
         if (plantVo.getEnabled()){
             return ResultBuilderVo.error("禁用状态的数据才能修改!");
         }
         //数据赋值
        PlantVo plantVo1 = new PlantVo();
         BeanUtils.copyProperties(plantUpdateVo,plantVo1);
         plantVo.setUpdateName("小明");
         plantVo.setUpdateTime(new Date());
         plantMapper.updatePlant(plantVo1);
        return ResultBuilderVo.success();
    }

    @Override
    @Transactional
    public ResultVo updateStatus(@Valid PlantUpdateStatusVo plantUpdateStatusVo) {
        PlantVo plantVo = plantMapper.queryPlantById(plantUpdateStatusVo.getId());
        if (plantVo==null){
            return ResultBuilderVo.error("修改数据不存在！");
        }
        //数据入库
        PlantVo plantVo1 = new PlantVo();
        BeanUtils.copyProperties(plantUpdateStatusVo,plantVo1);
        plantVo.setUpdateName("小明");
        plantVo.setUpdateTime(new Date());
        plantMapper.updateStatusPlant(plantVo1);
        return ResultBuilderVo.success();
    }

    public ResultVo validate(PlantAddVo plantAddVo){
        //验证法人是否存在
         int count = plantMapper.queryLegalPersonByCode(plantAddVo.getLegalPersonCode());
         if (count==0){
             return ResultBuilderVo.error("请验证法人信息，法人信息不存在！");
         }
        //验证工厂是否重复
        PlantVo plantcount = plantMapper.queryPlantByCode(plantAddVo.getLegalPlantCode());
        if (plantcount!= null){
            return ResultBuilderVo.error("工厂已经存在！");
        }
        return  ResultBuilderVo.success();
    }

}
